package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.code.UserCode;
import com.shangxian.giveithome.entity.*;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.FeiUserService;
import com.shangxian.giveithome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserFeiController {

  @Autowired
  private FeiUserService feiUserService;

  @Autowired
  private UserService userService;

  @Resource
  private MechanismServer mechanismServer;

  public Result update_role(RoleUser roleUser) {
    feiUserService.update_role(roleUser);
    return Result.update_success();
  }

  public UserDetails create(UserInfo userInfo) {
    return userService.create(userInfo);
  }


  public RolesUser get_role(Integer id) {
    return feiUserService.get_role(id);
  }


  public UserDetails queryByOpenId(String openid) {

    return feiUserService.queryByOpenId(openid);
  }

  public Result update_user_data(UserDataUpdate userDataUpdate) {
    feiUserService.update_user_data(userDataUpdate);
    return Result.success();
  }

  public Result PutUser(PutUserFei putUserFei) {
    feiUserService.updateUser(putUserFei);
    return Result.success();
  }

  public UserData queryUserData(Long id) {
    return userService.queryUserData(id);
  }

  public List<UserSimple> queryById(List<Long> ids) {
    List<UserSimple> userSimple = userService.queryByIds(ids);
    if (CollectionUtils.isEmpty(userSimple)) {
      return new ArrayList<>();
    }
    return userSimple;
  }

  public UserSimple queryUsById(Long id) {
    return userService.queryById(id);
  }

  public List<User> queryRole3Ids() {
    return userService.queryRole3();
  }

  public List<VolunteerUser> queryUserVolunteer(Long id) {
    List<VolunteerUser> volunteers = mechanismServer.queryByUserVolunteer(id);
    if (CollectionUtils.isEmpty(volunteers)) {
      return new ArrayList<>();
    }
    return volunteers;
  }

  @PutMapping(value = "/upUserVolunteerTime", name = "更新用户志愿时长信息")
  public Result PutUserVolunteerTime(@RequestBody PutUserFei putUserFei) {
    if (StringUtils.isEmpty(putUserFei.getOpenId())) {
      ExceptionUtils.error(UserCode.USER_UPDATE_ERROR);
    }
    return Result.success(feiUserService.updateUserVolunteerTime(putUserFei.getId()));
  }

  // 清空该用户在redis中的数据
  public void removeUserDetails(Long id) {
      userService.removeUserDetails(id);
  }

  // 修改用户数据，减少
  public Result updateUserDataReduce(UserDataUpdate userDataUpdate) {
    feiUserService.updateUserDataReduce(userDataUpdate);
    return Result.success();
  }
}
